package com.example.thread.d9CompletableFuture2.d4异步任务编排;

import com.example.thread.d9CompletableFuture2.CommonUtils;

import java.util.Arrays;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class d2_ThenComposeDemo1 {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        ExecutorService pool = Executors.newFixedThreadPool(4);
        CompletableFuture<String[]> future = CompletableFuture.supplyAsync(() -> {
            CommonUtils.printThreadLog("读取文件开始");
            String readFile = CommonUtils.readFile("读取文件,开始了");
            return readFile;
        }).thenCompose(content -> CompletableFuture.supplyAsync(() -> {
            CommonUtils.printThreadLog("读取转换中。。。");
            String[] split = content.split(",");
            return split;
        },pool));

        CommonUtils.printThreadLog("main continue");
        String[] strs = future.get();
        pool.shutdown();
        CommonUtils.printThreadLog("strs:"+ Arrays.toString(strs));

        /**
         * CompletableFutute<R> thenCompose(Function<T,CompletableFutute<R>> fn)
         * CompletableFutute<R> thenComposeAsync(Function<T,CompletableFutute<R>> fn)
         * CompletableFutute<R> thenComposeAsync(Function<T,CompletableFutute<R>> fn,Executor executor)
         */
    }
}
